package com.shiliao.app.activity.personal.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shiliao.app.R;
import com.shiliao.app.activity.Feedback.ui.AboutActivity;
import com.shiliao.app.activity.Feedback.ui.FeedbackDetailActivity;
import com.shiliao.app.activity.MainActivity;
import com.shiliao.app.activity.personal.presenter.PersonalPresenter;
import com.shiliao.app.activity.personal.view.PersonalView;
import com.shiliao.app.event.MessageEvent;
import com.shiliao.app.manager.YMUserManager;
import com.shiliao.app.update.UpdateEntity;
import com.shiliao.app.update.YimeiFileDownloadDialog;
import com.shiliao.app.utils.FileNameManage;
import com.shiliao.app.weight.ActionBarView;
import com.yimei.framework.base.event.BaseMessageEvent;
import com.yimei.framework.base.ui.BaseFragment;
import com.yimei.framework.util.LogUtil;
import com.yimei.framework.util.PreferenceHelper;
import com.yimei.framework.util.SystemUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yinzhiqiang on 2017/9/6.
 */

public class PersonalFragment extends BaseFragment implements PersonalView,
        MainActivity.IPageChangedListener,View.OnClickListener {

    private PersonalPresenter personalPresenter;

    @BindView(R.id.action_ba)
    ActionBarView mActionBar;

    @BindView(R.id.mine_user_name)
    TextView mNickName;//昵称

    @BindView(R.id.mine_phone)
    TextView mTelePhone;//手机号

    @BindView(R.id.tv_content_examine)
    TextView contentExamine;

    @BindView(R.id.tv_content_Referee)
    TextView contentReferee;

    @BindView(R.id.tv_contribution_num)
    TextView contributionNum;

    @BindView(R.id.mine_information)
    RelativeLayout mineInformation;

    @BindView(R.id.tv_feedback)
    TextView feedback;

    @BindView(R.id.tv_about)
    TextView about;

    @BindView(R.id.message)
    TextView mMessage;

    @BindView(R.id.tv_check_update)
    TextView checkUpdate;

    private YimeiFileDownloadDialog yimeiFileDownloadDialog;
    @Override
    public int getLayoutId() {
        return R.layout.activity_personal;
    }

    @Override
    public void onPageChanged(int position) {
        if (MainActivity.getCurrentView() == MainActivity.POSITION_PERSONAL) {
            if (getView() == null && getActivity() != null) {
                mContext = getActivity();
                initFragment();
            } else if (!isInit
                    && getView() != null) {
                initFragment();
            } else if (isInit && getView() != null) {
                //     requestData();
                initFragment();
            }
        }
    }

    @Override
    protected void initFragmentView() {
        super.initFragmentView();
        initFragment();
    }

    private void initFragment() {
        isInit = true;
        mActionBar.setTitles("个人中心");
        personalPresenter = new PersonalPresenter(this);
        if (MainActivity.getCurrentView() == MainActivity.POSITION_PERSONAL) {
            personalPresenter.request();
        }
        click();
        //added by luyi
        if (YMUserManager.bHasnewmessage)
            mMessage.setText("消息（有新消息）");
        else
            mMessage.setText("消息");
    }

    private void click(){
        mMessage.setOnClickListener(this);
        contentExamine.setOnClickListener(this);
        contentReferee.setOnClickListener(this);
        contributionNum.setOnClickListener(this);
        mineInformation.setOnClickListener(this);
        feedback.setOnClickListener(this);
        about.setOnClickListener(this);
        checkUpdate.setOnClickListener(this);
    }


//    @OnClick(R.id.message)
//    public void toMessage() {
//        toActivity(new Intent(getContext(), MessageActivity.class));
//    }

//    @OnClick(R.id.tv_contribution_num)
//    public void toContributionNum() {
//        toActivity(new Intent(getContext(), ContributionNumberActivity.class));
//    }

//    @OnClick(R.id.mine_information)
//    public void setInfo() {
//        toActivity(new Intent(getContext(), InforMationActivity.class));
//    }

    @Override
    public void onEventMainThread(BaseMessageEvent event) {

        if (event != null && event.eventName != null && event.eventName.equals(MessageEvent.EVENT_UPDATE_INFO)) {
            personalPresenter.request();
            LogUtil.e("yzq", "更新成功");
        }
    }

    @Override
    public String getUserId() {
        return YMUserManager.getUserId(getContext());
    }

    @Override
    public void setNickName(final String nickName, final String mPhone,String weixin) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                PreferenceHelper.write(getContext(), FileNameManage.PERSONAL_PAGE,
                        FileNameManage.PERSONAL_USER_NICK, nickName);//存储用户nick
                PreferenceHelper.write(getContext(), FileNameManage.PERSONAL_PAGE,
                        FileNameManage.PERSONAL_USER_PHONE_NM, mPhone);//存储用户手机号码

                PreferenceHelper.write(getContext(), FileNameManage.PERSONAL_PAGE,
                        FileNameManage.PERSONAL_USER_WEIXIN_NM, weixin);//存储用户微信号

                if (!TextUtils.isEmpty(nickName)) {
                    mNickName.setVisibility(View.VISIBLE);
                    mNickName.setText(nickName);
                }
                if (!TextUtils.isEmpty(mPhone)) {
                    mTelePhone.setVisibility(View.VISIBLE);
                    String phone = mPhone.substring(0, 3) + "****" + mPhone.substring(7, mPhone.length());
                    mTelePhone.setText(phone);
                }
            }
        });
    }

    @Override
    public void showDownloadDialog(UpdateEntity updateEntity) {
        if (updateEntity != null
                && updateEntity.getStatus() == 200
                && updateEntity.getVesion() > Float.valueOf(SystemUtils.getAppVersionName(getContext()))) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (yimeiFileDownloadDialog == null) {
                        yimeiFileDownloadDialog = new YimeiFileDownloadDialog(getActivity(), updateEntity);
                    } else {
                        yimeiFileDownloadDialog.setUpdateEntity(updateEntity);
                    }
                    if (!yimeiFileDownloadDialog.isShowing()) {
                        yimeiFileDownloadDialog.show(false);
                    }
                }
            });
        }else{
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showToast("当前是最新版本");
                }
            });

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.message://跳转消息
                toActivity(new Intent(getContext(), MessageActivity.class));
                break;
            case R.id.tv_contribution_num://跳转贡献点
                toActivity(new Intent(getContext(), ContributionNumberActivity.class));
                break;
            case R.id.mine_information://跳转设置头像
                toActivity(new Intent(getContext(), InforMationActivity.class));
                break;
            case R.id.tv_feedback://跳转意见反馈
                toActivity(new Intent(getContext(), FeedbackDetailActivity.class));
                break;
            case R.id.tv_about://跳转关于我们
                toActivity(new Intent(getContext(), AboutActivity.class));
                break;
            case R.id.tv_content_Referee://跳转内容推荐
                toActivity(new Intent(getContext(), ContentRefereeActivity.class));
                break;
            case R.id.tv_content_examine://跳转内容审核
                toActivity(new Intent(getContext(), ContentExamineActivity.class));
                break;
            case R.id.tv_check_update:
                personalPresenter.checkoutUpdate(SystemUtils.getAppVersionName(getContext()), YMUserManager.getUserId(getContext()));
                break;

        }
    }
}
